<?php

//小站选项模型
class Options_model extends Model {

	function Options_model()
	{
		parent::Model();	
		$this->load->database();
	}
	
	//添加选项
	function add_options($siteid, $opname)
	{
		$data = array(
			"siteid" => $siteid,
			"opname" => $opname,
			"addtime" => time(),
		);
		$result = $this->db->insert("site_options", $data);
		$opid = $this->db->insert_id();
		return $opid;
	}
	
	//获取小站选项
	function get_siteoptions($siteid)
	{
		$options = array();
		$query = $this->db->from("site_options")->where("siteid", $siteid)->order_by("id")->get();
		if($query->num_rows()>0) {
			$options = $query->result_array();
		}
		return $options;
	}
	
	//删除小站选项内容
	function del_siteoptions($siteid)
	{
		//获取选项
		$opid = $plid = $options = $plugins = array();
		$options = $this->get_siteoptions($siteid);
		foreach ($options as $option) {
			$opid[] = $option['id'];
		}
		//获取插件
		$query = $this->db->from("site_plugins")->where_in("opid", $opid)->order_by("id")->get();
		if($query->num_rows()>0) {
			$plugins = $query->result_array();
		}
		//删除内容
		if (empty($plugins)) return false;
		foreach ($plugins as $plugin) {
			$plid[] = $plugin['id'];
		}
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_hot');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_news');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_board');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_social');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_vote');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_special');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_album');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_video');
	}
	
	//获取第一个选项
	function get_firstoption($siteid)
	{
		$option = array();
		$query = $this->db->from("site_options")->where("siteid", $siteid)->order_by("id")->get();
		if($query->num_rows()>0) {
			$option = $query->row_array();
		}
		return $option;
	}
	
	//获取选项
	function get($opid)
	{
		$option = array();
		$query = $this->db->get_where("site_options", array("id"=>$opid));
//		echo $this->db->last_query();exit;
		if($query->num_rows()>0) {
			$option = $query->row_array();
		}
		return $option;
	}
	
	//编辑选项
	function edit_option($opid, $opname)
	{
		$data = array(
			"opname" => $opname,
			"addtime" => time(),
		);
		$this->db->where("id", $opid);
		$result = $this->db->update("site_options", $data);
		return $result;
	}
	
	//删除选项
	function del_option($opid)
	{
		$plugins = $plid = array();
		//删除选项内容
		//获取插件
		$query = $this->db->from("site_plugins")->where("opid", $opid)->order_by("id")->get();
		if($query->num_rows()>0) {
			$plugins = $query->result_array();
		}
		//删除内容
		if (empty($plugins)) return false;
		foreach ($plugins as $plugin) {
			$plid[] = $plugin['id'];
		}
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_hot');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_news');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_board');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_social');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_vote');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_special');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_album');
		$this->db->where_in('plid', $plid);
		$this->db->delete('module_video');
		
		$result = $this->db->delete("site_options", array("id"=>$opid));
		
		return $result;
	}
	
}

/* End of file */